/*
 *  $Id: AvatarOfNature.cs 1004 2010-05-03 11:50:53Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Earth.Spells {
    public class AvatarOfNature: SelfEnchantmentCard {
        public AvatarOfNature(): base("001_e_s_avatar_of_nature") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Earth;
            Value = 100;
        }

        protected override EnchantmentAffect CreateEnchantment() {
            return new AvatarOfNatureAffect(3);
        }

        public static void UseAffect(Player player) {
            AvatarOfNatureAffect affect = player.GetAffect<AvatarOfNatureAffect>();
            if (affect == null) return;
            affect.DecCount();
        }

        public class AvatarOfNatureAffect: EnchantmentAffect<AvatarOfNature> {
            private int _count;

            public AvatarOfNatureAffect(int count): base(true) {
                DefaultDescription = true;
                _count = count;
            }

            public override bool IsEnded {
                get { return _count == 0; }
            }

            public override string DescriptionText {
                get { return Description.GetText(_count); }
            }

            public override Affect Clone() {
                return new AvatarOfNatureAffect(_count);
            }

            public override void SetAffect() {
                base.SetAffect();
                GlobalSummary.OnManaChange += DoManaChange;
            }
            public override void ResetAffect() {
                base.ResetAffect();
                GlobalSummary.OnManaChange -= DoManaChange;
            }

            private void DoManaChange(Player player, ElementType element, int value) {
                if (player != Owner || element == ElementType.Earth || value >= 0) return;
                int dmg = (-value+1) / 2;
                player.DamageDirectBy(NewDamage(DamageType.Health, dmg), false);
            }

            public void DecCount() {
                if (_count > 0) _count--;
            }
        }
    }
}
